<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 9/6/2019
 * Time: 11:39 PM
 */

namespace p5\objects;


class FileLockUtils
{
    static public function getLockGo($path, $lockFile, $giveUpOnGetLockFailed, \Closure $next, $sleepULong = 50000, $removeLockFileOnFinish = true)
    {
        try {
            $fileLock = new FileLock($lockFile, $path);
            while ($fileLock->lock() === false) {
                if ($giveUpOnGetLockFailed === true) {
                    // echo "不等待锁了，直接放弃执行";
                    return;
                }
                usleep(50000);
            }
            $next();
        } catch (\Throwable $t) {
            
        } finally {
            if ($removeLockFileOnFinish) {
                $fileLock->releace();
            } else {
                $fileLock->releaceWithoutDeleteFile();
            }

        }
    }
}